package com.hudsonmq.spring.handler.process;

import com.hudson.codec.domain.RemotingCommand;
import com.hudson.common.domain.meta.TopicQueueMap;
import com.hudson.common.enums.MessageTypeEnum;
import com.hudson.core.json.JSONUtil;
import com.hudsonmq.spring.HudsonMQClientController;
import com.hudsonmq.spring.handler.Process;
import com.hudsonmq.spring.impl.produce.helper.TopicQueueHelper;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * @Author: 渔小鲤
 * @DateTime: 2025/7/25 22:54
 **/
@Component
public class QueueInfoHandler implements Process {
    private static final Logger logger = LoggerFactory.getLogger(QueueInfoHandler.class);

    @Override
    public String getType() {
        return MessageTypeEnum.QUEUE_MAP.getType();
    }

    @Override
    public RemotingCommand process(ChannelHandlerContext context, RemotingCommand remotingCommand) {
        logger.debug("【DEBUG】原始消息内容: " + remotingCommand); // 先打印原始消息
        TopicQueueMap topicQueueMap = JSONUtil.fromJson(new String(remotingCommand.getBody(), StandardCharsets.UTF_8), TopicQueueMap.class);
        TopicQueueHelper.changeTopicQueue(topicQueueMap);
        HudsonMQClientController.BROKER_TOPIC_QUEUQ_MAP.put(context.channel().id().asLongText(), topicQueueMap.getQueueNumMap());

        HudsonMQClientController.START_GET_QUEUE_MAP_COUNT_DOWN_LATCH.countDown();
        return null;
    }
}
